//--------------------------------------------------------------------------------------
// File: Tutorial07.fx
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
Texture2D txDiffuse : register( t0 );
SamplerState samLinear : register( s0 );
Texture3D voxelTexture : register( t1 );
SamplerState voxelSamplerLinear : register( s1 );

cbuffer cbNeverChanges : register( b0 )
{
    matrix View;
};

cbuffer cbChangeOnResize : register( b1 )
{
    matrix Projection;
};

cbuffer cbChangesEveryFrame : register( b2 )
{
    matrix World;
};


//--------------------------------------------------------------------------------------
struct VS_INPUT
{
    uint z8_y8_x8_case8 : TEXCOORD0;
};

struct PS_INPUT
{
    uint z8_y8_x8_null4_edge4 : TEXCOORD0;
    
};



//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PS_INPUT VS( VS_INPUT input )
{
    PS_INPUT output = (PS_INPUT)0;
	int cube_case = (int)(input.z8_y8_x8_case8 & 0xFF);
	int  bit0 = (cube_case     ) & 1;
	int  bit3 = (cube_case >> 3) & 1;
	int  bit1 = (cube_case >> 1) & 1;
	int  bit4 = (cube_case >> 4) & 1;
	int3 build_vert_on_edge = abs( int3(bit3,bit1,bit4) - bit0.xxx );

	uint bits = input.z8_y8_x8_case8 & 0xFFFFFF00;
	if (build_vert_on_edge.x != 0)
	bits |= 1;
	if (build_vert_on_edge.y != 0)
	bits |= 2;
	if (build_vert_on_edge.z != 0)
	bits |= 4;
	
	output.z8_y8_x8_null4_edge4 = bits;

    return output;
}
//--------------------------------------------------------------------------------------
// Geometry Shader
//--------------------------------------------------------------------------------------

[maxvertexcount(3)]
void GS( point PS_INPUT input[1], inout PointStream<PS_INPUT> triStream )
{
	PS_INPUT output;

	uint z8_y8_x8_null8 = input[0].z8_y8_x8_null4_edge4 & 0xFFFFFF00;

	if (input[0].z8_y8_x8_null4_edge4 & 1) {
	output.z8_y8_x8_null4_edge4 = z8_y8_x8_null8 | 2;
	triStream.Append(output);
	}
	if (input[0].z8_y8_x8_null4_edge4 & 2) {
	output.z8_y8_x8_null4_edge4 = z8_y8_x8_null8 | 1;
	triStream.Append(output);
	}
	if (input[0].z8_y8_x8_null4_edge4 & 4) {
	output.z8_y8_x8_null4_edge4 = z8_y8_x8_null8 | 4;
	triStream.Append(output);
	}
	
}
